﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AutoCode.Win;


namespace EZ.Open.AutoCode
{
    //后加的sqlite一些公共操作数据库的方法汇总
    public class SqliteDal
    {
        public static int GetBusinessID(string name)
        {
            DataSet bussinessinfo = SQLiteHelper.ExecuteDataSet("select * from ez_business where name = '" + name + "' ");
            try
            {
                return Convert.ToInt32(bussinessinfo.Tables[0].Rows[0]["id"]);
            }
            catch
            {
                return 0;
            }
        }

        public static int GetMethodID(string name)
        {
            DataSet methodinfo = SQLiteHelper.ExecuteDataSet("select * from ez_method where name = '" + name + "' ");
            try
            {
                return Convert.ToInt32(methodinfo.Tables[0].Rows[0]["id"]);
            }
            catch { return 0; }
        }

        public static int GetParamID(string name, int method_id)
        {
            DataTable dt = SQLiteHelper.ExecuteDataSet("select * from ez_param where name='" + name + "' and method_id=" + method_id + ";", null).Tables[0];
            try
            {
                return Convert.ToInt32(dt.Rows[0]["id"]);
            }
            catch { return 0; }
        }

        public static DataTable GetModelsCustom()
        {
            DataTable dt = SQLiteHelper.ExecuteDataSet("select * from ez_model where ifcustom<>0;", null).Tables[0];
            return dt;
        }

        public static DataTable GetModelAttrs(string modelid)
        {
            DataTable dt = SQLiteHelper.ExecuteDataSet("select * from ez_model_attr where model_id='" + modelid + "';", null).Tables[0];
            return dt;
        }

        public static IList<SqliteModelInfo> TableListGet()
        {
            var rs = new List<SqliteModelInfo>();
            //SQLiteHelper.ExecuteNonQuery("insert into ez_model(name,note) values ('" + modelName + "','" + item.TableNote + "实体')");
            foreach (DataRow dr in GetModelsCustom().Rows)
            {
                var r = new SqliteModelInfo();
                r.Name = Convert.ToString(dr["name"]);
                r.Note = Convert.ToString(dr["note"]);
                r.ModelID = Convert.ToString(dr["id"]);
                r.Tables = "";
                rs.Add(r);
            }
            return rs;
        }

        public static IList<SqliteModelParamInfo> TableFieldListGet(string modelid)
        {
            //   SQLiteHelper.ExecuteNonQuery("insert into ez_model_attr(name,note,type,not_null,default_value,model_id) 
            //values('" + list_field[i].ColName + "','" + list_field[i].PropValue + "','" + list_field[i].ColType + "'," 
            //+ list_field[i].IsNullAble + ",''," + model_id + ");", null);
            var rs = new List<SqliteModelParamInfo>();
            foreach (DataRow dr in GetModelAttrs(modelid).Rows)
            {
                var r = new SqliteModelParamInfo();
                r.ColName = Convert.ToString(dr["name"]);
                r.PropValue = Convert.ToString(dr["note"]);
                r.ColType = Convert.ToString(dr["type"]);
                r.IsNullAble = Convert.ToString(dr["not_null"]);
                r.DefaultValue = Convert.ToString(dr["default_value"]);
                r.ModelID = Convert.ToString(dr["model_id"]);
                r.ParamID = Convert.ToString(dr["id"]);
                rs.Add(r);
            }
            return rs;
        }
    }
}
